package com.example.xinlijiankangsys.mapper;

import com.example.xinlijiankangsys.pojo.Comment;
import com.example.xinlijiankangsys.pojo.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PostMapper {
    @Insert("INSERT INTO post_tb(user_id, title, content, create_time) " +
            "VALUES(#{userId}, #{title}, #{content}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "postId")
    int insert(Post post);

    @Select("SELECT * FROM post_tb WHERE post_id = #{postId} ORDER BY create_time DESC")
    @Results(id = "postResultMap", value = {
            @Result(property = "postId", column = "post_id", id = true),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "title", column = "title"),
            @Result(property = "content", column = "content"),
            @Result(property = "likeCount", column = "like_count"),
            @Result(property = "createTime", column = "create_time")
    })
    Post selectById(Long postId);

    @Select("SELECT * FROM post_tb ORDER BY create_time DESC LIMIT #{offset}, #{pageSize}")
    @ResultMap("postResultMap")
    List<Post> selectByPage(@Param("offset") int offset, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM post_tb")
    int selectTotalCount();

    @Update("UPDATE post_tb SET like_count = like_count + 1 WHERE post_id = #{postId}")
    int incrementLikeCount(Long postId);

    @Delete("DELETE FROM post_tb WHERE post_id = #{postId}")
    int deleteById(Long postId);
}